home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / prolog / modprolg / mod-prol.lha / Prolog / sim / unify.c < prev   
C/C++ Source or Header  |  1991-12-11  |  6KB  |  138 lines

  1. /************************************************************************
  2. *                                                                       *
  3. * The SB-Prolog System                                                  *
  4. * Copyright SUNY at Stony Brook, 1986; University of Arizona, 1987      *
  5. *                                                                       *
  6. ************************************************************************/
  7.  
  8. /*-----------------------------------------------------------------
  9. SB-Prolog is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY.  No author or distributor
  11. accepts responsibility to anyone for the consequences of using it
  12. or for whether it serves any particular purpose or works at all,
  13. unless he says so in writing.  Refer to the SB-Prolog General Public
  14. License for full details.
  15.  
  16. Everyone is granted permission to copy, modify and redistribute
  17. SB-Prolog, but only under the conditions described in the
  18. SB-Prolog General Public License.   A copy of this license is
  19. supposed to have been given to you along with SB-Prolog so you
  20. can know your rights and responsibilities.  It should be in a
  21. file named COPYING.  Among other things, the copyright notice
  22. and this notice must be preserved on all copies. 
  23. ------------------------------------------------------------------ */
  24. /* unify.c */
  25.  
  26. #include "sim.h"
  27. #include "aux.h"
  28.  
  29. extern double floatval();
  30.  
  31. unify(op1, op2)
  32. LONG op1, op2;
  33. {
  34.    register LONG_PTR top;
  35.    WORD     arity, i;
  36.  
  37. nun: 
  38.    switch (TAG(op1)) {
  39.       case FREE:                              /* op1 = free var */
  40.          NDEREF(op1, nun);
  41. nun1:    switch (TAG(op2)) {
  42.             case FREE:                        /* op1 = free, op2 = free */
  43.                NDEREF(op2, nun1);
  44.                if (op1 != op2) {
  45.                   if (op1 < op2) {            /* op1 < op2 */
  46.                      if (op1 < (LONG)hreg) {  /* op1 not in loc stack */
  47.                         FOLLOW(op2) = op1;
  48.                         PUSHTRAIL(op2);
  49.              } else {                 /* op1 points to op2 */
  50.                         FOLLOW(op1) = op2;
  51.                         PUSHTRAIL(op1);
  52.              }
  53.                   } else {                    /* op1 > op2 */
  54.                      if (op2 < (LONG)hreg) {  /* op2 not in loc stack */
  55.                         FOLLOW(op1) = op2;
  56.                         PUSHTRAIL(op1);
  57.              } else {                 /* op2 points to op1 */
  58.                         FOLLOW(op2) = op1;
  59.                         PUSHTRAIL(op2);
  60.              }
  61.                   }
  62.                }
  63.                return 1;
  64.             case CS  :                        /* op1 = free, op2 = con/str */
  65.             case LIST:                        /* op1 = free, op2 = list    */
  66.             case NUM :                        /* op1 = free, op2 = num     */
  67.                FOLLOW(op1) = op2;
  68.                PUSHTRAIL(op1);
  69.                return 1;
  70.          }
  71.  
  72.       case CS:                                /* op1 = con/str */
  73. nun2:    switch (TAG(op2)) {
  74.             case FREE:                        /* op1 = con/str, op2 = free */
  75.                NDEREF(op2, nun2);
  76.                FOLLOW(op2) = op1;
  77.                PUSHTRAIL(op2);
  78.                return 1;
  79.             case CS:                          /* op1 = con/str, op2 = con/str */
  80.                if (op1 != op2) {              /* a != b */
  81.                   UNTAG(op1);
  82.                   UNTAG(op2);
  83.                   if (FOLLOW(op1) != FOLLOW(op2)) /* 0(a) != 0(b) */
  84.                      return 0;
  85.                   else {
  86.              arity = GET_STR_ARITY(op1);
  87.                      for (i = 1; i <= arity; i++) 
  88.                         if (!unify(*((LONG_PTR)op1 + i), *((LONG_PTR)op2 + i)))
  89.                            return 0;
  90.                   }
  91.                }
  92.                return 1;
  93.             case LIST:                        /* op1 = con/str, op2 = list */
  94.             case NUM :                        /* op1 = con/str, op2 = num  */
  95.                return 0;
  96.          }
  97.  
  98.       case LIST:                              /* op1 = list */
  99. nun3:    switch (TAG(op2)) {
  100.             case FREE:                        /* op1 = list, op2 = free */
  101.                NDEREF(op2, nun3);
  102.                FOLLOW(op2) = op1;
  103.                PUSHTRAIL(op2);
  104.                return 1;
  105.             case CS :                         /* op1 = list, op2 = con/str */
  106.             case NUM:                         /* op1 = list, op2 = num     */
  107.                return 0;
  108.             case LIST:                        /* op1 = list, op2 = list */
  109.                if (op1 != op2) {
  110.                   UNTAG(op1);
  111.                   UNTAG(op2);
  112.                   if (!unify(*(LONG_PTR)op1, *(LONG_PTR)op2) ||
  113.                       !unify(*(((LONG_PTR)op1)+1), *(((LONG_PTR)op2)+1)))
  114.                      return 0;
  115.                }
  116.                return 1;
  117.          }
  118.  
  119.       case NUM:                               /* op1 = num */
  120. nun4:    switch (TAG(op2)) {
  121.             case FREE:                        /* op1 = num, op2 = free */
  122.                NDEREF(op2, nun4);
  123.                FOLLOW(op2) = op1;
  124.                PUSHTRAIL(op2);
  125.                return 1;
  126.             case NUM:                         /* op1 = num, op2 = num */
  127.                if (op1 == op2)                /* op1 = op2 */
  128.           return 1;
  129.                else if ((ISFLOAT(op1) || ISFLOAT(op2)) &&
  130.                 NUMVAL(op2) == NUMVAL(op1))
  131.                   return 1;
  132.             case CS  :                        /* op1 = num, op2 = con/str */
  133.             case LIST:                        /* op1 = num, op2 = list    */
  134.                return 0;
  135.          }
  136.    }  /* end of switch on op1 */
  137. }  /* end of unify */
  138.